rmprop

Override all properties inhereted by an object's prototype chain
If you've ever wanted to use property names for other purposes such as the .length
property on an Array
or String
, this library will transform your object (eg: an instance of a class) into an object with all properties found in its prototype chain set to undefined
, effectively stopping the javascript engine from bubbling up the prototype chain to find the values for those properties.
This module was created to support meta-programming in javascript; it is useful for creating API objects in which a property name is set dynamically and used like a dictionary (ie: when the property name could be any string). While Proxies are not yet supported, this might suffice as an alternative to your trap needs.
Install
$ npm install --save rmprop
Usage
rmprop(thing: anytype[, exclude: array{string}])
Finds all properties defined by each class in thing
's prototype chain, then creates a virtual copy of thing
object with all those properties set to undefined
. Not only does this block the prototype properties from being accessed, but it also lets you define properties that the original object might not allow (such as the .length
property). The optional exclude
argument declares which properties not to override.
var rmprop = require('rmprop');
var arr = rmprop(['hello','there'], ['indexOf']);
arr.forEach;
arr[0];
arr.length;
arr.indexOf('there');
arr.join;
arr.reverse;
arr.prototype;
arr.__proto__;
rmprop.real
This is a Symbol which allows you to access the original (real) value of thing
that was given to rmprop. Eg:
var rmprop = require('rmprop');
var arr = rmprop(['hi','there']);
Object.defineProperty(arr, 'length', {
get: function() {
return this[unprop.real].join('').length;
},
})
arr[0];
arr.length;
arr.indexOf;
arr[rmprop.real].length;
arr[rmprop.real].indexOf;
rmprop.emulateArray(arr: array, cover: object)
Special method for copying all methods from array arr
onto object cover
. Useful for creating an array that can also be a function:
let a = ['a', 'b', 'c'];
let f = rmprop.emulateArray(a, function(name) {
return 'hello '+name+'!';
});
f('you');
f.length;
f[0];
f.indexOf('b');
f.push('d');
f.length;
f[3];
f.length = 0;
f.push('a');
f.length;
f[0];
Testing equality
rmprop
creates a virtual copy of the real object, meaning that it creates a property/method on a new object for each corresponding property/method on the original object. The object you get back from rmprop will never be identical to the one you gave it.
Consider the following example:
var obj = {test: 'hi'};
var virtual = rmprop(obj);
virtual.test;
(virtual === obj);
(virtual[unprop.real] === obj);
However, excluding the valueOf
property on primitive datatypes will yield true when the loose equality operator is used:
var num = unprop(5, ['valueOf']);
(num == 5);
(num === 5);
typeof num;
Transparent properties/methods
The virtual copy returned by rmprop
keeps any properties not defined in thing
's prototype chain and treats them as transparent proxies for the original object. This means that changes to the virtual copy will update the original object.
var obj = {test: 'hi'};
var virtual = rmprop(obj);
virtual.test = 'hello';
obj.test;
virtual.test;
Primitive datatypes
Primitive datatypes get boxed into their object equivalent (eg: 25
becomes Number(25)
) so that they can hold properties.
var str = rmprop('hi', ['valueOf']);
(str == 'hi');
(str === 'hi');
(str[rmprop.real] === 'hi');
str.length;
str.indexOf;
str.__proto__;
str instanceof String;
License
ISC © Blake Regalia